package mest.service.cxj.impl;

import mest.VO.cxj.ProductOrderVO;
import mest.VO.cxj.ProductJobVO;
import mest.dao.cxj.OrderDao;
import mest.entity.plan_purchase;
import mest.entity.plan_takematerial;
import mest.entity.product_job;
import mest.entity.product_order;
import mest.service.cxj.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-21 0:05
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao iod;

    @Override
    public int findProductOrderNumber() {
        return iod.findProductOrderNumber();
    }

    @Override
    public List<ProductJobVO> selectProductInfo(int index, int pageSize) {
        return iod.selectProductInfo((index-1)*pageSize,pageSize);
    }

    @Override
    public int selectProductInfoCount() {
        int i = iod.selectProductInfoCount();
        return i;
    }

    @Override
    public int insertProductOrder(product_order po) {
        int i;
        try {
            i =  iod.insertProductOrder(po);
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public int insertProductJob(List<ProductJobVO> spJVO) {
        int i;
        try {
            i =  iod.insertProductJob(spJVO);
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public List<ProductOrderVO> findAllOrder(int index, int pageSize) {
        return iod.findAllOrder((index-1)*pageSize,pageSize);
    }

    @Override
    public int findAllOrderCount() {
        return iod.findAllOrderCount();
    }


    //审核功能
    @Override
    public int check(plan_purchase pp) {
        try {
            //1.根据poId录入Plan_purchase
            int i1 = iod.insertPlan_Purchase(pp);
            //2.更新字典表
            int i2 = iod.updatePlan_PurchaseNumber();
            //3.一对多查询相应的PoId生产单对应的所有作业对象
            List<product_job> allproduct_jobByPoId = iod.findAllproduct_jobByPoId(pp.getPoId());
            for(product_job pj : allproduct_jobByPoId)
                System.out.println(pj);
            //4.批量录入生产作业的物料到采购内容中
            int i4 = iod.insertAllJobtoPlan_productJob_purchase(pp.getId(),allproduct_jobByPoId);
            //5.插入领料单
            plan_takematerial pt = new plan_takematerial();
            pt.setPoId(pp.getPoId());
            int i5 = iod.insertPlan_TakeMaterial(pt);
            //6.领料字典+1
            int i6 = iod.updatePlan_TakeMaterialNumber();
            //7.批量录入生产作业的成品信息到领料内容中
            int i7 = iod.insertAllJobtoPlan_productJob_TakeMaterial(pt.getId(),allproduct_jobByPoId);

        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }


}
